package org.example;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.options.UiAutomator2Options;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

import java.net.URL;
/**
 * author: zhanggw
 * 创建时间:  2022/3/25
 */
@Slf4j
public class AppiumTest1 {
    public static void main(String[] args) {
        // 初始化UiAutomator2Options
        UiAutomator2Options options = new UiAutomator2Options();

        // 设置平台名称
        options.setPlatformName("Android");
        // 设置平台版本
        options.setPlatformVersion("7");
        // 设置设备名称
        options.setDeviceName("1");

        // 设置APP信息
        options.setAppPackage("com.android.browser");
        options.setAppActivity("com.android.browser.BrowserActivity");

        //创建AppiumDriver对象，连接上Appium服务器，并将相应的键值对传过去
        AndroidDriver driver = null;

        try {
            driver = new AndroidDriver(new URL("http://localhost:4723/wd/hub"), options);

            Thread.sleep(3000);

            driver.findElement(By.id("com.android.browser:id/url")).click();
            WebElement element = driver.findElement(By.className("android.widget.EditText"));
            element.sendKeys("https://yjeju.rq9eyvt.store/s97b1gyl?_360safeparam=29903015");
            //使用 Actions 类
            Actions actions = new Actions(driver);
            actions.sendKeys(Keys.ENTER).perform();

            Thread.sleep(20000);
        } catch (Exception e) {
//            logger.error("appiium异常,测试用例不通过!", e);
            throw new RuntimeException(e);
        } finally {
            if (driver != null) {
                driver.quit();
            }
        }
    }
}
